package everyday;

import javax.swing.plaf.IconUIResource;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Day08
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        long[] arr = new long[num];
        for (int i = 0;i < num;i++){
            arr[i] = in.nextLong();
        }
        if (num == 1){
            System.out.println(num);
            System.out.println(arr[0]);
        }
        // 1 2 3 4 5 6
        Stack<Long> stack = new Stack<>();
        Stack<Long> tmp = new Stack<>();
        stack.push(arr[0]);
        for (int i = 1;i < arr.length;i++){
            long temp = stack.pop();
            if (temp < arr[i]){
                int size = stack.size();
                while(size-- > 0){
                    long de = stack.pop();
                    if (de >= arr[i]){
                        stack.push(de);
                        break;
                    }
                }
                stack.push(arr[i]);
            }else{
                stack.push(temp);
                stack.push(arr[i]);
            }
        }
        System.out.println(stack.size());
        for(long s : stack){
            System.out.print(s+" ");
        }
        in.close();
    }
}
